package stardust.product.platform.webc;


import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import stardust.product.platform.webc.annotation.Handler;
import stardust.product.platform.webc.annotation.Parameter;
import stardust.product.platform.webc.annotation.Renderer;
import stardust.product.platform.webc.log.AccessLog;


/**
 * 用于记录资源入口点映射的类型和方法。
 * <p/>
 * @author ChenChang
 */
public class WebcResourceReference
{

  private String uri;

  private Class<?> targetClass;
  private Method targetMethod;
  private Object instance;

  private Handler handler;
  private Parameter[] parameter;
  private Renderer renderer;
  
  private AccessLog accessLog;

  public WebcResourceReference(String uri, Class<?> targetClass, Method targetMethod)
  {
    this.uri = uri;
    this.targetClass = targetClass;
    this.targetMethod = targetMethod;

    /*
     * 获取方法的入口点和响应标注。
     */
    this.handler = this.targetMethod.getAnnotation(Handler.class);
    this.renderer = this.targetMethod.getAnnotation(Renderer.class);

    /*
     * 获取方法参数的标注。
     */
    Annotation[][] annotationses = this.targetMethod.getParameterAnnotations();
    Annotation[] ant;

    this.parameter = new Parameter[this.targetMethod.getParameterAnnotations().length];

    for (int i = 0; i < annotationses.length; i++)
    {
      ant = annotationses[i];

      for (int j = 0; j < ant.length; j++)
      {
        if (ant[j] instanceof Parameter)
        {
          this.parameter[i] = (Parameter) ant[j];
        }
      }
    }
    
    this.accessLog = new AccessLog();
  }
  
  public boolean isRESTfulURI()
  {
    return this.uri.indexOf('{') != -1;
  }

  public void setInstance(Object instance)
  {
    this.instance = instance;
  }

  public Object getInstance()
  {
    return this.instance;
  }
  
  public void setUri(String uri)
  {
    this.uri = uri;
  }

  public String getUri()
  {
    return this.uri;
  }
  
  public Class<?> getTargetClass()
  {
    return this.targetClass;
  }

  public Method getTargetMethod()
  {
    return this.targetMethod;
  }

  public Handler getEntryPoint()
  {
    return this.handler;
  }

  public Parameter[] getEntryParam()
  {
    return this.parameter;
  }

  public Renderer getEntryResult()
  {
    return this.renderer;
  }

  public AccessLog getAccessLog()
  {
    return accessLog;
  }
  
}
